package com.kaishengit.service;

import java.text.ParseException;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.kaishengit.dao.ContactDao;
import com.kaishengit.dao.DealDao;
import com.kaishengit.dao.NoteDao;
import com.kaishengit.pojo.Contact;
import com.kaishengit.pojo.Deal;
import com.kaishengit.pojo.Note;
import com.kaishengit.util.DataUtil;



@Transactional
public class NoteService {
	private DealDao dealDao;
	private ContactDao contactDao;
	private NoteDao noteDao;
	
	/**
	 * 保存一条关于deal的记录
	 * @param note
	 * @param dealid
	 * @param contactid
	 * @throws ParseException
	 */
	public void saveNote(Note note, String dealid, String contactid) throws ParseException {
		Deal deal = dealDao.findByUnique("id",dealid);
		if(contactid.equals("")){
			note.setType("note");
		}else{
			Contact contact = contactDao.findByUnique("id", contactid);
			note.setContact(contact);
			note.setType("noteaboutcontact");
		}
		String time ;
		System.out.println(note.getCreatetime());
		if(note.getCreatetime().equals("")){
			time = DataUtil.getNow();
		}else{
			time = DataUtil.noteStarttime(note.getCreatetime());
		}
		note.setCreatetime(time);
		note.setDeal(deal);
		noteDao.save(note);
		
	}
	/**
	 * 根据dealId 查找所有的Note
	 * @param id
	 * @return
	 */
	/*public List<Note> findNoteByDealId(String id) {
		Deal deal = dealDao.findByUnique("id", id);
		return noteDao.findAllByProperty("deal",deal);
	}*/
	
	
	//set 
	

	public void setDealDao(DealDao dealDao) {
		this.dealDao = dealDao;
	}

	public void setContactDao(ContactDao contactDao) {
		this.contactDao = contactDao;
	}
	public void setNoteDao(NoteDao noteDao) {
		this.noteDao = noteDao;
	}
	public List<Note> findNoteByDealId(String id) {
		Deal deal = dealDao.findByUnique("id",id);
		return noteDao.findAllByProperty("deal",deal);
	}
	public void delNote(String id) {
		noteDao.del(id);
		
	}
	public Note findNote(String id) {
		return noteDao.findByUnique("id",id);
		
	}
	public void update(Note note) {
		noteDao.save(note);
		
	}
	


	
	
}
